"""
Description:
Author: 月间
Date: 2025-05-19 23:53:48
LastEditTime: 2025-09-30 19:50:09
LastEditors:
"""

from fastapi import APIRouter, Depends
from fastapi_utils.cbv import cbv
from sqlalchemy.ext.asyncio import AsyncSession
from src import schemas, models
from src.core.response.response_schema import (
    ResponseGenericSchema,
    PageResponse,
)
from authx.dependencies.permission import PermissionChecker
from src.db.db_database import alchemy
from src.core.base_controller import BaseController
from typing import Annotated
from src.schemas.task_schema import OperationRecordParam, LoginRecordParam
from src.services.login_record_service import LoginRecordService
from src.services.operation_record_service import OperationRecordService

router = APIRouter()


@cbv(router)
class RecordController(BaseController):
    db: AsyncSession = Depends(alchemy.provide_session())

    # 操作日志
    @router.get(
        "/operate/list",
        response_model=PageResponse[schemas.OperationRecordSchema],
        summary="获取操作日志列表",
        dependencies=[Depends(PermissionChecker("system:record:list"))],
    )
    async def get_operation_record_list(
        self,
        param: Annotated[OperationRecordParam, Depends()],
        record_service: Annotated[
            OperationRecordService, Depends(alchemy.provide_service(OperationRecordService))
        ],
    ):
        where = await param.get_where_and_limit_offset(models.OperationRecord)
        res, count = await record_service.list_and_count(
            *where,
            param.order_by("created_at"),
            count_with_window_function=True,
        )
        return self.paginated_response(
            data=res, total=count, current=param.page, size=param.limit
        )

    @router.get(
        "/login/list",
        response_model=PageResponse[schemas.LoginRecordOut],
        summary="获取登录日志列表",
        dependencies=[Depends(PermissionChecker("system:record:list"))],
    )
    async def get_login_record_list(
        self,
        param: Annotated[LoginRecordParam, Depends()],
        login_service: Annotated[
            LoginRecordService, Depends(alchemy.provide_service(LoginRecordService))
        ],
    ):
        where = await param.get_where_and_limit_offset(models.LoginRecord)
        res, count = await login_service.list_and_count(
            *where,
            param.order_by("created_at"),
            count_with_window_function=True,
        )
        return self.paginated_response(
            data=res, total=count, current=param.page, size=param.limit
        )
